package com.example.joker.abao00;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.joker.abao00.model.Cookbook;
import com.example.joker.abao00.model.Msg;

import java.util.List;

/**
 * Created by Joker on 2015/12/19.
 */
public class MessageAdapter extends ArrayAdapter<Msg> {
    private int resourceId;
    public MessageAdapter(Context context, int textViewResource, List<Msg> objects) {
        super(context, textViewResource, objects);
        resourceId=textViewResource;}

    /**
     * 重写了getView()方法，这个方法在每个子项被滚动到屏幕内的时候
     *会被调用
     */
    @Override
    public View getView(int position,View convertView,ViewGroup parent){
        Msg msg=getItem(position);
        View view;
        ViewHolder viewHolder;
        if(convertView==null){
            view= LayoutInflater.from(getContext()).inflate(resourceId,null);
            viewHolder=new ViewHolder();
            viewHolder.leftRelativeLayout =(RelativeLayout)view.findViewById(R.id.left_relative_layout);
            viewHolder.rightRelativeLayout =(RelativeLayout)view.findViewById(R.id.right_relative_layout);
            viewHolder.leftMsg=(TextView)view.findViewById(R.id.left_msg);
            viewHolder.rightMsg=(TextView)view.findViewById(R.id.right_msg);
            view.setTag(viewHolder);
        }else {
            view=convertView;
            viewHolder=(ViewHolder)view.getTag();
        }
        if(msg.getType()==msg.TYPE_RECEIVED){
            viewHolder.leftRelativeLayout.setVisibility(View.VISIBLE);
            viewHolder.rightRelativeLayout.setVisibility(View.GONE);
            viewHolder.leftMsg.setText(msg.getQuestionText());
            /**
             * 根据code值确定应该采用哪种方式输出
             */
            if(msg.getCode()==100000){
                viewHolder.leftMsg.setText(msg.getReplyText());
            }else if(msg.getCode()==200000){
                viewHolder.leftMsg.setText(msg.getReplyText()+msg.getURL());
            }else if(msg.getCode()==308000){
                List<Cookbook> cookbooksLists=msg.getList().subList(0,2);   //subList返回列表中指定的之间的List部分。不然显示的数据太长了
                StringBuilder sb=new StringBuilder();
                for(Cookbook c:cookbooksLists){
                    sb.append(" "+c.getName()+":"+c.getInfo()+" 详情请点击:"+c.getDetailurl()+"\n");
                }

                viewHolder.leftMsg.setText(msg.getReplyText()+sb);
            }else {    //开场问候没有code 必须单独else
                viewHolder.leftMsg.setText(msg.getReplyText());
            }
        }

        if(msg.getType()==msg.TYPE_SEND){
            viewHolder.rightRelativeLayout.setVisibility(View.VISIBLE);
            viewHolder.leftRelativeLayout.setVisibility(View.GONE);
            viewHolder.rightMsg.setText(msg.getQuestionText());

        }
        return view;
    }
    class ViewHolder{
        RelativeLayout leftRelativeLayout;
        RelativeLayout rightRelativeLayout;
        TextView leftMsg;
        TextView rightMsg;
    }
}
